﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Threading;

using LongNet.Demo;
using LongNet.Util;
using LongNet.Spider;
namespace LongNet.Main
{
    class Program
    {
        //static LogManager.Logger logger = LogManager.GetCurrentClassLogger();
        static void Main(string[] args)
        {
            try
            {
                /*
                //模拟中国人口变化
                //Population.PrintPoputation();
                */
                /*
                 //UDP Hole
                Console.Write("请选择:1,服务器2,客户端:");
                string input = Console.ReadLine();
                if (input == "1")
                {
                    LongNet.P2PServer.AppClass.StartServer();
                }
                else if (input == "2")
                {                  
                    LongNet.P2PClient.AppClass.StartClient();
                }     
                */               
                
                WebSiteDownloaderOptions options =
              new WebSiteDownloaderOptions();

                options.DownloadUri =
                    new Uri(@"http://www.kundao888.com.cn/");
                options.DestinationFolderPath =
                    new DirectoryInfo(@"D:\temp\www.kundao888.com.cn");

                WebSiteDownloader downloader =
                    new WebSiteDownloader(options);

                downloader.ProcessingUrl +=
                    new WebSiteDownloader.ProcessingUrlEventHandler(
                    downloader_ProcessingUrl);

                downloader.ProcessCompleted +=
                    new WebSiteDownloader.ProcessCompletedEventHandler(
                    downloader_ProcessCompleted);

                downloader.ProcessAsync();

                while (true)
                {
                    Thread.Sleep(1000);
                    Console.WriteLine(@".");

                    lock (typeof(Program))
                    {
                        if (finished)
                        {
                            break;
                        }
                    }
                }

                Console.WriteLine(@"finished.");
              
                /*
                HttpPost();
                */
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
           
            Console.ReadKey();
        }

        private static void downloader_ProcessingUrl(
            object sender,
            WebSiteDownloader.ProcessingUrlEventArgs e)
        {
            Console.WriteLine(
                string.Format(
                @"Processing URL '{0}'.", e.UriInfo.AbsoluteUri));
        }

        private static bool finished;

        private static void downloader_ProcessCompleted(
            object sender,
            RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Console.WriteLine(@"Error: " + e.Error.Message);
            }
            else if (e.Cancelled)
            {
                Console.WriteLine(@"Canceled");
            }
            else
            {
                Console.WriteLine(@"Success");
            }

            lock (typeof(Program))
            {
                finished = true;
            }
        }

        private static void HttpPost()
        {
            string url = "http://china.alibaba.com/message/send_feedback_esite.htm?iframe_delete=true&toId=tjy92&topDomainFlag=Y";
            string postData = "&action=AskQuoteAction&event_submit_do_sendEsite=anything&toId=tjy92&sid=UsJ4we2CMgJduxh7C5YLxPEw1-Qj1IS0N-O4s1&visitType&topDomainFlag=Y&title=企业网站留言&_fmme.send._0.n=1&_fmme.send._0.s=male&_fmme.send._0.t=222&_fmme.send._0.m=22&_fmme.send._0.e=22@163.com&_fmme.send._0.a=11&_fmme.send._0.me=1222222222&_fmme.send._0.v=9026&Submit=留 言";
            var result = Utils.HttpPost(url, postData);
            Console.Write(result);
        }
    }   
}
